//
//  GGZFBBusinessViewController.m
//  Gunterapple
//
//  Created by 热带雨林 on 2020/4/1.
//  Copyright © 2020 热带雨林. All rights reserved.
//

#import "GGZFBBusinessViewController.h"
#import "GGAddtion.h"
#import "GGZFBBusinessModel.h"
#import "GGZFBBusinessCell.h"
#import "ZFBBusinessHeaderView.h"
static NSString *cellID = @"cellID";
@interface GGZFBBusinessViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) NSMutableArray <GGZFBBusinessModel *>*businessModelList;
@property (nonatomic, strong) UITableView *tableView;
@end

@implementation GGZFBBusinessViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"商圈";
    self.view.backgroundColor = [UIColor whiteColor];
    [self loadData];
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[GGZFBBusinessCell class] forCellReuseIdentifier:cellID];
    [self setHeaderView];
    //上拉加载 下拉刷新
    MJRefreshNormalHeader *headerMJ = [[MJRefreshNormalHeader alloc]init];
    [headerMJ setRefreshingTarget:self refreshingAction:@selector(headerRefresh)];
    self.tableView.mj_header = headerMJ;
    
    MJRefreshBackNormalFooter *footerMJ = [[MJRefreshBackNormalFooter alloc]init];
    [footerMJ setRefreshingTarget:self refreshingAction:@selector(footerRefresh)];
    self.tableView.mj_footer = footerMJ;
}
- (void)setHeaderView{
    ZFBBusinessHeaderView *header = [[ZFBBusinessHeaderView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 350 + 300)];
    self.tableView.tableHeaderView = header;
}
- (void)headerRefresh{
    [self loadData];
    [self.tableView.mj_header endRefreshing];
}
- (void)footerRefresh{
    GGZFBBusinessModel *model = [[GGZFBBusinessModel alloc]init];
    model.name = @"肯德基(黑马程序员店)";
    model.icon = @"kfc";
    model.distance = 10;
    model.discount = @"黑马程序员学员免费";
    model.offNum = 3;
    model.averagePrice = 1;
    model.level = 5;
    [self.businessModelList addObject:model];
    [self.tableView reloadData];
    [self.tableView.mj_footer endRefreshing];
    //数据加载之后往上滚动一行
    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:self.businessModelList.count - 1 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.businessModelList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    GGZFBBusinessCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    cell.model = self.businessModelList[indexPath.row];
    return cell;
}
- (void)loadData{
    [self.businessModelList removeAllObjects];
    self.businessModelList = [NSArray gg_arrayModelWithPlistName:@"business.plist" withClassName:@"GGZFBBusinessModel"].mutableCopy;
    [self.tableView reloadData];
}
- (UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, SJHeight, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)-IphoneXTabbarH) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.estimatedRowHeight = 200;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    }
    return _tableView;
}
- (NSMutableArray <GGZFBBusinessModel *>*)businessModelList{
    if(!_businessModelList){
        _businessModelList = [NSMutableArray array];
    }
    return _businessModelList;
}

@end
